#region License and Copyright

/*
 * Copyright (C) 2008 Nathan Palmer
 * http://www.nathanpalmer.com
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author:
 *     Nathan Palmer <nathan1@nathanpalmer.com>
 */

#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Amazon.S3.com.amazonaws.s3;

namespace Amazon.S3
{
    public class S3MetadataCollection : IEnumerable<S3Metadata>, IEnumerator<S3Metadata>
    {
        private SortedList<string, S3Metadata> sortedList;

        public S3MetadataCollection()
        {
            sortedList = new SortedList<string, S3Metadata>();
        }

        public S3MetadataCollection(MetadataEntry[] Metadata)
            : this()
        {
            foreach (MetadataEntry entry in Metadata)
            {
                sortedList.Add(entry.Name, new S3Metadata(entry.Name, entry.Value));
            }
        }

        public void Add(string Name, string Value)
        {
            sortedList.Add(Name, new S3Metadata(Name, Value));
        }

        public void Remove(string Name)
        {
            sortedList.Remove(Name);
        }

        public int Count
        {
            get
            {
                return sortedList.Count;
            }
        }

        public string this[string Name]
        {
            get
            {
                return ((S3Metadata)sortedList[Name]).Value;
            }
        }

        public MetadataEntry[] ToMetadataEntry()
        {
            MetadataEntry[] entries = new MetadataEntry[sortedList.Count];
            int currentIndex = 0;

            foreach (KeyValuePair<string, S3Metadata> entry in sortedList)
            {
                entries[currentIndex] = new MetadataEntry();
                entries[currentIndex].Name = entry.Key;
                entries[currentIndex].Value = entry.Value.Value;
            }

            return entries;
        }


        #region IEnumerable<S3Metadata> Members

        public IEnumerator<S3Metadata> GetEnumerator()
        {
            return (IEnumerator<S3Metadata>)this;
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion

        #region IEnumerator<S3Metadata> Members

        public S3Metadata Current
        {
            get { return sortedList.GetEnumerator().Current.Value; }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion

        #region IEnumerator Members

        object IEnumerator.Current
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public bool MoveNext()
        {
            return sortedList.GetEnumerator().MoveNext();
        }

        public void Reset()
        {
            sortedList.GetEnumerator().Reset();
        }

        #endregion
    }
}
